package com.saop.core.aspectj;

import com.saop.annotation.AopDialogAfter;
import com.saop.core.api.DialogListener;
import com.saop.core.api.DialogParameter;
import com.saop.core.api.DialogParameterCallback;
import com.saop.core.utils.DialogUtils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class DialogAfterAspectJ {


    @Pointcut("execution(@com.saop.annotation.AopDialogAfter !synthetic *.new(..))")
    public void constructor() {
    }  //构造方法切入点


    @Pointcut("execution(@com.saop.annotation.AopDialogAfter !synthetic * *(..))")
    public void method() {
    }  //方法切入点

    @Around("( method() || constructor() ) && @annotation(dialog)")
    public Object syncDialogAroundJoinPoint(ProceedingJoinPoint joinPoint, AopDialogAfter dialog) throws Throwable {
        // 获取方法中是否有DialogCallback回调
        DialogListener callback = null;
        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof DialogListener) {
                callback = (DialogListener) arg;
                break;
            }
        }
        Object result = null;
        try {
            // 先执行方法体内容
            result = joinPoint.proceed();
            return result;
        } finally {
            // 如果没有回调 ,也可以显示 ,只是选择就无意义了
            final DialogListener finalCallback = callback;
            final Object finalResult = result;
            final DialogListener temp = new DialogListener() {
                @Override
                public boolean isShow(DialogParameter parameters, Object result) {
                    if (finalCallback != null) {
                        return finalCallback.isShow(parameters, finalResult);
                    }
                    return true;
                }

                @Override
                public void onPositive(String positive) {
                    if (finalCallback != null) {
                        finalCallback.onPositive(positive);
                    }
                }

                @Override
                public void onNegative(String negative) {
                    if (finalCallback != null) {
                        finalCallback.onNegative(negative);
                    }
                }

                @Override
                public void onNeutral(String neutral) {
                    if (finalCallback != null) {
                        finalCallback.onNeutral(neutral);
                    }
                }

                @Override
                public void onCancel() {
                    if (finalCallback != null) {
                        finalCallback.onCancel();
                    }
                }
            };
            DialogUtils.dialog().setParameter(new DialogParameterCallback() {
                @Override
                public void reset(DialogParameter parameters) {
                    parameters.setTitle(dialog.title());
                    parameters.setMessage(dialog.message());
                    parameters.setPositive(dialog.positive());
                    parameters.setNegative(dialog.negative());
                    parameters.setNeutral(dialog.neutral());
                    parameters.setTouchOutSideCancel(dialog.touchOutSideCancel());
                }
            }).callback(temp).show();
        }
    }
}
